package getLucky;

import java.util.SplittableRandom;

public class Solution {
    public int getLucky(String s, int k) {
        s = get(s);
        while (k>0) {
            s = rev(s);
            k--;
        }
        return Integer.parseInt(s);
    }
    private String rev(String t) {
        // 转换成各位数字之和
        String ans = "0";
        for (int i = 0; i < t.length(); i++) {
            ans = add(ans, t.charAt(i)+"");
        }
        return ans;
    }
    private String add(String a,String b) {
        StringBuilder ans = new StringBuilder();
        int i = a.length()-1;
        int j = b.length()-1;
        int t = 0; // 进位
        while (i>=0||j>=0||t!=0) {
            int sum = t;
            if(i>=0) {
                sum+=(a.charAt(i)-'0');
            }
            if(j>=0) {
                sum+=(b.charAt(j)-'0');
            }
            ans.append((char) (sum%10+'0'));
            t = sum/10;
        }
        return ans.reverse().toString();
    }
    private String get(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            sb.append(s.charAt(i)-'a'+1);
        }
        return sb.toString();
    }
}
